package com.example.administrator.managesystem.presenter.profile;

import android.content.Context;
import android.util.Log;

import com.example.administrator.managesystem.bean.DeleteGoodsBean;
import com.example.administrator.managesystem.bean.GoodsListBean;
import com.example.administrator.managesystem.bean.LandListBean;
import com.example.administrator.managesystem.bean.UpdateGoodsBean;
import com.example.administrator.managesystem.bean.UploadBean;
import com.example.administrator.managesystem.bean.UploadGoodsBean;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.ui.GoodsView;
import com.example.administrator.managesystem.utils.ToastUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class GoodsPresenter {
    private GoodsView goodsView;

    public GoodsPresenter(GoodsView goodsView) {
        this.goodsView = goodsView;
    }

    public void landListRequest(String user_id, String token, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).landListRequest(user_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<LandListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(LandListBean landListBean) {
                        if(landListBean.getStatus()==200){
                            if(landListBean.getData().isEmpty()){
                                ToastUtils.showToast(context,"请添加土地！");
                                return;
                            }
                            goodsView.showLandGrid(landListBean.getData());

                        }
                    }

                });


    }

    public void checkVPRequest(String user_id, String land_id,String token, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).checkVPRequest(user_id,land_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<UploadBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(UploadBean uploadBean) {
                        if(uploadBean.getStatus()==200){
                            goodsView.showUpload(uploadBean.getData());
                        }
                    }

                });


    }

    public void sendGoodsRequest(String user_id, String token, String land_id, String title, String price, String number, String url, List<String> list, final Context context){

        File video = new File(url);
        final RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), video);

        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part video_body = MultipartBody.Part.createFormData("video", video.getName(), requestFile);


        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        //.addFormDataPart(ParamKey.TOKEN, token);//ParamKey.TOKEN 自定义参数key常量类，即参数名
        //多张图片
        for (int i = 0; i < list.size(); i++) {
            File file = new File(list.get(i));//filePath 图片地址
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("images", file.getName(), imageBody);//"imgfile"+i 后台接收图片流的参数名
        }
        List<MultipartBody.Part> parts = builder.build().parts();


        ServiceManager.getInstance(context).getService(GuideAPI.class).addGoodsRequest(user_id,token,land_id,title,price,number,video_body,parts)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<UploadGoodsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(UploadGoodsBean uploadGoodsBean) {
                       if(uploadGoodsBean.getStatus()==200){
                           ToastUtils.showToast(context,uploadGoodsBean.getData());
                              goodsView.showDeleteMsg();
                       }else{
                           ToastUtils.showToast(context,uploadGoodsBean.getMsg());
                           goodsView.showDeleteMsg();
                       }


                    }

                });


    }
    public void sendGoodsRequestOK(String user_id, String token, String land_id, String title, String price, String number, final Context context){

        ServiceManager.getInstance(context).getService(GuideAPI.class).addGoodsRequestOK(user_id,token,land_id,title,price,number)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<UploadGoodsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                        goodsView.showDeleteMsg();
                    }

                    @Override
                    public void onNext(UploadGoodsBean uploadGoodsBean) {
                        if(uploadGoodsBean.getStatus()==200){
                            ToastUtils.showToast(context,uploadGoodsBean.getData());
                                     goodsView.showDeleteMsg();
                        }else{
                            ToastUtils.showToast(context,uploadGoodsBean.getMsg());
                            goodsView.showDeleteMsg();
                        }


                    }

                });


    }
    public void goodsListRequest(String user_id,String token,int page,int pageSize,final int type, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).goodsListRequest(user_id,token,page,pageSize)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<GoodsListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(GoodsListBean goodsListBean) {
                       if(goodsListBean.getStatus()==200){
                           goodsView.showGoodsList(goodsListBean.getData(),type);
                       }else{
                           ToastUtils.showToast(context,goodsListBean.getMsg());
                       }
                    }

                });


    }
    public void deleteGoodsRequest(String user_id,String token,List<Integer> list, final Context context){
        Log.e("msg","listid:"+list);
        ServiceManager.getInstance(context).getService(GuideAPI.class).deleteGoodsRequest(user_id,token,list)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<DeleteGoodsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(DeleteGoodsBean deleteGoodsBean) {
                     if(deleteGoodsBean.getStatus()==200){
                         ToastUtils.showToast(context,deleteGoodsBean.getData());
                         goodsView.showDeleteMsg();
                     }else{
                         ToastUtils.showToast(context,deleteGoodsBean.getMsg());
                     }
                    }

                });
    }

    public void updateGoodsRequest(String user_id,String token,int sale_id, String number,String price,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).updateGoodsRequest(user_id,sale_id,token,price,number)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<UpdateGoodsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(UpdateGoodsBean updateGoodsBean) {
                        if (updateGoodsBean.getStatus()==200){
                            ToastUtils.showToast(context,updateGoodsBean.getData());
                            goodsView.showUpdateMsg();
                        }else{
                            ToastUtils.showToast(context,updateGoodsBean.getMsg());
                            goodsView.showErrMsg();
                        }
                    }

                });


    }

}
